//备注：拷贝代码请加上作者信息
//作者：王海涛
//邮箱：1126471088@qq.com
//版本：V1.0.0
#include "time_slice_task_os.h"

__weak unsigned int WHT_SysTick_ms;

typedef struct
{
    WHT_Task_Function_t task;//任务
    unsigned int state;      //状态
    unsigned int cycle;      //周期
    unsigned int last_run;   //上次运行
}WHT_Task_t;


static WHT_Task_t WHT_Task[WHT_Time_Slice_Task_Count];


/* 空闲任务 */
__weak void WHT_Idle_Task(void)
{

}

/* 安装任务 */
static int WHT_Install_Task(WHT_Task_Function_t task)
{
    int err = -1;

    if (task != (void*)0)
    {
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].task == (void*)0)
            {
                WHT_Task[i].task = task;
                WHT_Task[i].state = 0;
                WHT_Task[i].cycle = 0xffffffffUL;
                err = 0;
                break;
            }
        }
    }
    return err;
} 
/* 移除任务 */
static int WHT_Remove_Task(WHT_Task_Function_t task)
{
    int err = -1;

    if (task != (void*)0)
    {
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].task == task)
            {
                WHT_Task[i].task = (void*)0;
                WHT_Task[i].state = 0;
                err = 0;
                break;
            }
        }
    }
    return err;
}
/* 设置任务周期 */
static int WHT_Set_Task_Cycle(WHT_Task_Function_t task, unsigned int ms)
{
    int err = -1;

    if (task != (void*)0)
    {
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].task == task)
            {
                WHT_Task[i].cycle = ms;
                err = 0;
                break;
            }
        }
    }
    return err;
}  
/* 开始任务 */
static int WHT_Start_Task(WHT_Task_Function_t task)
{
    int err = -1;

    if (task != (void*)0)
    {
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].task == task)
            {
                WHT_Task[i].last_run = WHT_SysTick_ms;
                WHT_Task[i].state = 1;
                err = 0;
                break;
            }
        }
    }
    return err;
}
/* 暂停任务 */
static int WHT_Stop_Task(WHT_Task_Function_t task)
{
    int err = -1;

    if (task != (void*)0)
    {
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].task == task)
            {
                WHT_Task[i].state = 0;
                err = 0;
                break;
            }
        }
    }
    return err;
}
/* 循环执行任务调度器 */
static void WHT_Loop_Task(void)
{
    static unsigned int Tick;

    if (Tick != WHT_SysTick_ms)
    {
        Tick = WHT_SysTick_ms;
        for (unsigned int i = 0; i < sizeof(WHT_Task) / sizeof(WHT_Task[0]); i++)
        {
            if (WHT_Task[i].state != 0)
            {
                if (Tick - WHT_Task[i].last_run >= WHT_Task[i].cycle)
                {
                    WHT_Task[i].task();
                    WHT_Task[i].last_run = Tick;
                }
            }
        }
    }
    else
    {
        WHT_Idle_Task();
    }
}



const WHT_Time_Slice_Task_OS_t WHT_Time_Slice_Task_OS =
{
    .install   = WHT_Install_Task,
    .remove    = WHT_Remove_Task,
    .set_cycle = WHT_Set_Task_Cycle,
    .start     = WHT_Start_Task,
    .stop      = WHT_Stop_Task,
    .loop      = WHT_Loop_Task,
};
